package com.jyserver.common.AutoCode.jybaits;

import com.jyserver.common.AutoCode.AutoCodeStartUp;
import com.jyserver.common.utils.ChineseCharToEnUtil;
import com.jyserver.common.utils.JyString;
import com.jyserver.common.utils.JyTime;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.mybatis.generator.api.IntrospectedTable;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;

public class GeneratorController {
    private static Logger logger = Logger.getLogger(GeneratorController.class);

    public void GeneratorController(IntrospectedTable arg1, String GeneratorController_targetPackage, String controllerhead) {
        System.err.println("生成控制类1");
        System.err.println(arg1.getPrimaryKeyColumns());

        MyJavaFormatter compilationUnit = new MyJavaFormatter(arg1, GeneratorController_targetPackage, controllerhead);
        compilationUnit.setRequiresRoles(Boolean.valueOf(AutoCodeStartUp.context.getProperty("RequiresRoles")));
        compilationUnit.setRequiresRoles(Boolean.valueOf(AutoCodeStartUp.context.getProperty("RequiresPermissions")));
        String str = compilationUnit.getallstr();
        try {
            String[] paths = GeneratorController_targetPackage.replace(".", "/").split("/");
            String path = "src/main/java/";
            for (int i = 0; i < paths.length; i++) {
                path = path + "/" + paths[i];
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdir();
                }
            }
            FileWriter fw = new FileWriter("src/main/java/" + GeneratorController_targetPackage.replace(".", "/") + "/" + compilationUnit.getFilename());
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void createData() {
        FileWriter fw = null;
        try {
            DecimalFormat df = new DecimalFormat("######0.00");
            String str = "[";
            fw = new FileWriter("E:\\project.db\\jiaoyu\\BaseData\\capacity.json");
            long starTime = JyTime.getStampLong() / 1000 - 10 * 60 * 60 * 24;
            for (int i = 0; i < 10; i++) {
                double a = (1 + Math.random() * (100));
                String date = null;
                try {
                    long time = starTime + i * 60 * 60 * 24;
                    date = JyTime.StampToDate(time * 1000);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                str = str + "{\"date\":\"" + date + "\",\"number\":\"" + df.format(a) + "\"},\n";
            }
            fw.write(str + "]");
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void complaintData() {
        FileWriter fw = null;
        try {
            DecimalFormat df = new DecimalFormat("######0.00");
            String str = "[";
            fw = new FileWriter("E:\\project.db\\jiaoyu\\BaseData\\complaint.json");
            long starTime = JyTime.getStampLong() / 1000 - 10 * 60 * 60 * 24;
            for (int i = 0; i < 10; i++) {
                double a = (1 + Math.random() * (5));
                String date = null;
                try {
                    long time = starTime + i * 60 * 60 * 26;
                    date = JyTime.StampToDate(time * 1000);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String jidian = JyString.getrandomStr(10);
                String plot = JyString.getrandomStr(9);
                String problem = JyString.getrandomStr(9);
                String address = JyString.getrandomStr(15);

                String biaoshi = JyString.getrandomStr(7);

                str = str + "{\"time\":\"" + date + "\",\"jidi\":\"" + jidian + ",\"plot\":\"" + plot + "\",\"problem\":\"" + problem + "\",\"address\":\"" + address + "\"" + "\",\"address\":\"" + address + "\"";
            }
            fw.write(str + "]");
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void StatementData() {
        FileWriter fw = null;
        try {
            DecimalFormat df = new DecimalFormat("######0.00");
            String str = "[";
            fw = new FileWriter("E:\\project.db\\jiaoyu\\BaseData\\capacity.json");
            long starTime = JyTime.getStampLong() / 1000 - 10 * 60 * 60 * 24;
            String columns = "";
            String[] title = new String[]{"卡顿时长占比", "比特率", "屏幕清晰度", "视频编码算法", "无线连通率", "无线掉线率", "流量", "切换率", "开始时间", "结束时间", "上行流量", "下行流量", "上行数据包"};
            title = new String[]{"无线连通率", "无线接通率", "切换成功率"};
            for (int n = 0; n < title.length; n++) {
                columns = columns + "<el-table-column\n" +
                        "      prop=\"" + ChineseCharToEnUtil.getFirstSpell(title[n]) + "\"\n" +
                        "      label=\"" + title[n] + "\">\n" +
                        "    </el-table-column>\n";
            }

            for (int i = 0; i < 20; i++) {
                double a = (1 + Math.random() * (5));
                String date = null;
                try {
                    long time = starTime + i * 60 * 60 * 26;
                    date = JyTime.StampToDate(time * 1000);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String item = "{\"time\":\"" + date + "\",";
                for (int n = 0; n < title.length - 1; n++) {
                    item = item + "\"" + ChineseCharToEnUtil.getFirstSpell(title[n]) + "\":\"" + JyString.getrandomNumber(2) + "\",";
                }
                str = str + item + "\"" + ChineseCharToEnUtil.getFirstSpell(title[title.length - 1]) + "\":\"" + JyString.getrandomNumber(2) + "\"},\n";
            }
            fw.write(str + "]\n" + columns);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void StatementDatawe() {
        FileWriter fw = null;
        try {
            DecimalFormat df = new DecimalFormat("######0.00");
            String str = "[";
            fw = new FileWriter("E:\\project.db\\jiaoyu\\BaseData\\PieChart.json");
            long starTime = JyTime.getStampLong() / 1000 - 10 * 60 * 60 * 24;
            String columns = "";
            String[] title = new String[]{"卡顿时长占比", "比特率", "屏幕清晰度", "视频编码算法", "无线连通率", "无线掉线率", "流量", "切换率", "开始时间", "结束时间", "上行流量", "下行流量", "上行数据包"};
            title = new String[]{"问题描述", "可能产生原因", "原因解释", "解决方案"};
            for (int n = 0; n < title.length; n++) {
                columns = columns + "<el-table-column\n" +
                        "      prop=\"" + ChineseCharToEnUtil.getFirstSpell(title[n]) + "\"\n" +
                        "      label=\"" + title[n] + "\">\n" +
                        "    </el-table-column>\n";
            }

            for (int i = 0; i < 20; i++) {
                double a = (1 + Math.random() * (5));
                String date = null;
                try {
                    long time = starTime + i * 60 * 60 * 26;
                    date = JyTime.StampToDate(time * 1000);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String item = "{";
                for (int n = 0; n < title.length - 1; n++) {
                    String name = "";
                    if (n == 0) {
                        name = "问题描述" + i;
                    } else if (n == 1) {
                        name = "可能产生原因" + i;
                    } else if (n == 2) {
                        name = "原因解释" + i;
                    } else if (n == 3) {
                        name = "";
                    }
                    item = item + "\"" + ChineseCharToEnUtil.getFirstSpell(title[n]) + "\":\"" + name + "\",";
                }
                str = str + item + "\"" + ChineseCharToEnUtil.getFirstSpell(title[title.length - 1]) + "\":\"" + JyString.getrandomNumber(2) + "\"},\n";
            }
            fw.write(str + "]\n" + columns);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}